#!/bash/bin
function s1_read_last_two_bytes_of_a_mbr_backup_file(){
	local file_path=$1
	local last_bytes=$(tail -c 2 "$file_path" | od -A n -t x1 | tr -d ' \n')
	if [ "$last_bytes" = "55aa" ];then
		echo "YES"
	else
		echo "NO"
	fi

	
}
function s2_read_mbr_partition_table_info(){
	#path="$1"
	#partition_table_start=$(hexdump -s 446 0n 16 -e '/1 "%02x "' "$path" | sed 's/ $//')
	#for i in {0..3}; do
	#offset=$((partition_table_start + i * 16))
	#start_sector=$(hexdump -s $offset -n 4 -e '/1 "%d\n"' "$path")
	#end_sector=$(hexdump -s $((offset +4)) -n 4 -e '/1 "%d\n"' "$path")
	#echo "$start_sector $end
for i in {0..3}; do		
offset=$((446 + i * 16))		
start_sector=$(dd if="$1" bs=1 skip=$((offset + 8)) count=4 2>/dev/null | od -t u4 -An | tr -d ' \n')		
if ! [[ $start_sector =~ ^[0-9]+$ ]];then			
continue		
fi		
if [ "$start_sector" -eq 0 ];then			
continue		
fi		
size_sector=$(dd if="$1" bs=1 skip=$((offset + 12)) count=4 2>/dev/null | od -t u4 -An | tr -d ' \n')		
end_sector=$((start_sector + size_sector - 1))		
echo "$start_sector $end_sector"	
done
}
function s3_parse_partition_info(){
	fdisk_output=$(cat)
	disk_size_bytes=$(echo "$fdisk_output" | grep "Disk /dev/vda:" | awk '{print $5}')
	disk_size_sectors=$(echo "$fdisk_output" | grep "Disk /dev/vda:" | awk '{print $7}')
	physical_sector_size=$(echo "$fdisk_output" | grep "Sector size" | awk '{print $4}')
	first_partition_start_sector=$(echo "$fdisk_output" | grep "/dev/vda1" | awk '{print $3}')
	first_partition_end_sector=$(echo "$fdisk_output" | grep "/dev/vda1" | awk '{print $4}')
	first_partition_type=$(echo "$fdisk_output" | grep "/dev/vda1" | awk '{print $8}')
	echo "disk size in bytes:$disk_size_bytes"
	echo "disk size in sectors:$disk_size_sectors"
	echo "physical sector size in bytes:$physical_sector_size"
	echo "first partition start sector id:$first_partition_start_sector"
	echo "first partition end sector id:$first_partition_end_sector"
	echo "first partition type:$first_partition_type"
}
function s4_run_passwd_as_plain_user_and_get_status(){
	#oversleeping_path=$(which oversleeping)
	#sudo chown root:root $oversleeping_path
	#sudo chmod 755 $oversleeping_path
	#sudo chmod u+s $oversleeping_path
	
	#nohup $oversleeping_path
	#function get_process_ids(){
	#local process_name=$1
	#local ids=$(ps -o uid,euid,ruid -C $process_name --no-headers)
	#IFS='' read -r -a id_arry <<< "$ids"
	#for id in "$(id_arry[@])"; do
	#username=$(id -nu $id)
	#echo -n "$id,$username,"
	#done
	#echo 
	#get_process_ids "oversleeping"
	oversleeping_path=$(which oversleeping)
	chown root "$oversleeping_path"
	chmod 755 "$oversleeping_path"
	$oversleeping_path &
	ps -o uid,euid,ruid -C 'oversleeping' | awk 'NR>1 {print $1","$2","$3}'	
}

function s5_add_a_scheduled_task(){
	echo "*/5 * * * * date +\%Y\%m\%d_\%H\%M\%S >> $HOME/test_crontab" | crontab
}
#ceshi

